﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Smugmug.Tests
{
    [TestClass]
    public class TestWebClientTests
    {
        public TestContext TestContext { get; set; }

        [TestMethod]
        public void IsMatch_TwoEqualUrls_returnsTrue()
        {
            Uri url = new Uri("http://www.mysite.com/test/?val1=1&val2=2");
            Assert.IsTrue(TestWebClient.IsMatch(url, url));
        }

        [TestMethod]
        public void IsMatch_UrlsWithDifferentSchemes_ReturnsFalse()
        {
            Uri url1 = new Uri("http://www.mysite.com/test/?val1=1&val2=2");
            Uri url2 = new Uri("https://www.mysite.com/test/?val1=1&val2=2");
            Assert.IsFalse(TestWebClient.IsMatch(url1, url2));
        }

        [TestMethod]
        public void IsMatch_UrlsWithDifferentPaths_ReturnsFalse()
        {
            Uri url1 = new Uri("http://www.mysite.com/test1/?val1=1&val2=2");
            Uri url2 = new Uri("http://www.mysite.com/test2/?val1=1&val2=2");
            Assert.IsFalse(TestWebClient.IsMatch(url1, url2));
        }

        [TestMethod]
        public void IsMatch_UrlsWithDifferentQueryStrings_ReturnsFalse()
        {
            Uri url1 = new Uri("http://www.mysite.com/test/?val1=1&val2=2");
            Uri url2 = new Uri("http://www.mysite.com/test/?val1=1&val2=3");
            Assert.IsFalse(TestWebClient.IsMatch(url1, url2));
        }

        [TestMethod]
        public void IsMatch_QueryStringWithWildcard_ExpandsCorrectly()
        {
            Uri url1 = new Uri("http://www.mysite.com/test/?val1=1&val2=2");
            Uri url2 = new Uri("http://www.mysite.com/test/?val1=1&val2=*");
            Assert.IsTrue(TestWebClient.IsMatch(url1, url2));
        }

        [TestMethod]
        public void IsMatch_MultipleWildcards_ExpandsCorrectly()
        {
            Uri url1 = new Uri("http://www.mysite.com/test/?val1=ThisIsAReallyLongString&val2=AnotherLongValue");
            Uri url2 = new Uri("http://www.mysite.com/test/?val1=*&val2=*");
            Assert.IsTrue(TestWebClient.IsMatch(url1, url2));
        }

        [TestMethod]
        public void IsMatch_MultipleWildcards_ParametersInDifferentOrder_ExpandsCorrectly()
        {
            Uri url1 = new Uri("http://www.mysite.com/test/?val1=ThisIsAReallyLongString&val2=AnotherLongValue");
            Uri url2 = new Uri("http://www.mysite.com/test/?val2=*&val1=*");
            Assert.IsTrue(TestWebClient.IsMatch(url1, url2));
        }

        [TestMethod]
        public void IsMatch_SameNumberOfQueryStringItems_ButDifferentKeys_ReturnsFalse()
        {
            Uri url1 = new Uri("http://www.mysite.com/test/?val2=1");
            Uri url2 = new Uri("http://www.mysite.com/test/?Val1=1");
            Assert.IsFalse(TestWebClient.IsMatch(url1, url2));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void IsMatch_WildcardInValue_ThrowsException()
        {
            Uri url1 = new Uri("http://www.mysite.com/test/?1=*");
            Uri url2 = new Uri("http://www.mysite.com/test/?*=2");
            TestWebClient.IsMatch(url1, url2);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void IsMatch_WildcardInKey_ThrowsException()
        {
            Uri url1 = new Uri("http://www.mysite.com/test/?key!=1");
            Uri url2 = new Uri("http://www.mysite.com/test/?key!=2");
            TestWebClient.IsMatch(url1, url2);
        }

        [TestMethod]
        public void IsMatch_PatternWithEmptyValue_DoesNotReturnIncorrectly()
        {
            Uri url1 = new Uri("http://www.mysite.com/test/?A=");
            Uri url2 = new Uri("http://www.mysite.com/test/?A=");
            Assert.IsTrue(TestWebClient.IsMatch(url1, url2));
        }

        [TestMethod]
        public void IsMatch_PatternWithOptionalKey_MatchesUrlWithoutThatKey()
        {
            Uri url1 = new Uri("http://www.mysite.com/test/?A=val");
            Uri url2 = new Uri("http://www.mysite.com/test/?A=val&b!=val");
            Assert.IsTrue(TestWebClient.IsMatch(url1, url2));
        }
    }
}
